package cn.dnui_zll731.dnuifood_731zlll.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.CommentsBean;
import cn.dnui_zll731.dnuifood_731zlll.bean.Result;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.UserModel;

public class OrderAdapter extends RecyclerViewAdapter<OrderAdapter.ViewHolder> implements RetrofitListener<Result> {


    public OrderAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final CommentsBean entity = (CommentsBean)data.get(position);
        holder.time.setText(entity.getOrder_time());
        holder.foodname.setText("菜品: "+entity.getFoodname());
        holder.shopname.setText("【"+entity.getShopname()+"】");
        holder.price.setText(entity.getPrice());
        if (entity.getContent()==null){  //如果用户评价了
            holder.btn_pingjia.setVisibility(View.GONE);
        }
        holder.btn_pingjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText editText = new EditText(context);
                editText.setText(entity.getContent());
                new AlertDialog.Builder(context).setTitle("评论")
                        .setView(editText)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (editText.getText().toString().trim()!=null){
                                    doPinglun(entity.getItem_id(),editText.getText().toString());//修改评价
                                }else{
                                    Toast.makeText(context,"请输入有效评论信息",Toast.LENGTH_SHORT).show();
                                }

                            }
                        }).setNegativeButton("取消",null)
                        .show();
            }
        });
    }
    private void doPinglun(String item_id, String editText){
        UserModel userModel = new UserModel();
        userModel.insertComment(item_id,editText,this);
    }

    @Override
    public void onSuccess(Result object, int flag) {
        if (object.getSuccess().equals("1")){
            Toast.makeText(context,"评论成功",Toast.LENGTH_SHORT).show();
            notifyDataSetChanged();//强制调用getView（）刷新item内容
        }else {
            Toast.makeText(context,"评论失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFalure() {
        Toast.makeText(context,"网络连接失败",Toast.LENGTH_SHORT).show();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView time,foodname,price,shopname;
        private Button btn_pingjia;

        public ViewHolder(View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.dd_time);
            foodname = itemView.findViewById(R.id.dd_foodname);
            price = itemView.findViewById(R.id.dd_jiage);
            shopname = itemView.findViewById(R.id.dd_shopname);
            btn_pingjia = itemView.findViewById(R.id.dd_btn);
        }

    }
}
